{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 原创：公众号《数据不吹牛》"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 天气爬虫"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests\n",
    "from lxml import etree\n",
    "\n",
    "def parse(url = 'https://www.tianqi.com/hangzhou'):\n",
    "    headers = {'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36'}\n",
    "    html = requests.get(url,headers = headers)\n",
    "    bs = etree.HTML(html.text)\n",
    "    \n",
    "    #今天天气相关数据：日期，星期几，天气，最低气温，最高气温\n",
    "    today_date = bs.xpath('//ul[@class = \"week\"]/li[1]/b/text()')[0]\n",
    "    today_week = bs.xpath('//ul[@class = \"week\"]/li[1]/span/text()')[0]\n",
    "    today_weather = bs.xpath('//ul[@class = \"txt txt2\"]/li[1]/text()')[0]\n",
    "    today_low = bs.xpath('//div[@class = \"zxt_shuju\"]/ul/li[1]/b/text()')[0]\n",
    "    today_high = bs.xpath('//div[@class = \"zxt_shuju\"]/ul/li[1]/span/text()')[0]\n",
    "\n",
    "    #明天天气相关数据，维度和上述一致\n",
    "    tomorrow_date = bs.xpath('//ul[@class = \"week\"]/li[2]/b/text()')[0]\n",
    "    tomorrow_week = bs.xpath('//ul[@class = \"week\"]/li[2]/span/text()')[0]\n",
    "    tomorrow_weather = bs.xpath('//ul[@class = \"txt txt2\"]/li[2]/text()')[0]\n",
    "    tomorrow_low = bs.xpath('//div[@class = \"zxt_shuju\"]/ul/li[2]/b/text()')[0]\n",
    "    tomorrow_high = bs.xpath('//div[@class = \"zxt_shuju\"]/ul/li[2]/span/text()')[0]\n",
    "    \n",
    "    tomorrow = ('明天是%s，%s,%s，%s-%s度，温差%d度')% \\\n",
    "          (tomorrow_date,tomorrow_week,tomorrow_weather,tomorrow_low,tomorrow_high,int(int(tomorrow_high)-int(tomorrow_low)))\n",
    "    \n",
    "    print(('明天是%s，%s,%s，%s-%s度，温差%d度')% \\\n",
    "          (tomorrow_date,tomorrow_week,tomorrow_weather,tomorrow_low,tomorrow_high,int(int(tomorrow_high)-int(tomorrow_low))))\n",
    "    \n",
    "    #计算今明两天温度差异，这里用的是最高温度\n",
    "    temperature_distance = int(tomorrow_high) - int(today_high)\n",
    "    \n",
    "    if temperature_distance > 0:\n",
    "        a = '明日升温%d' % temperature_distance\n",
    "        print('明日升温%d' % temperature_distance)\n",
    "    if temperature_distance < 0:\n",
    "        a = '明日降温%d' % temperature_distance\n",
    "        print('明日降温%d' % temperature_distance)\n",
    "    else:\n",
    "        a = '最高气温不变'\n",
    "        print('最高气温不变')\n",
    "    content = tomorrow,a\n",
    "    return content"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 展示爬取结果"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "明天是11月19日，星期二,晴转多云，5-14度，温差9度\n",
      "明日降温-3\n"
     ]
    }
   ],
   "source": [
    "#默认爬取杭州，可以找到自己城市所对应的地址\n",
    "weather = parse()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 邮件发送"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "import yagmail\n",
    "\n",
    "def send_email(contents,send_to = 'receiver_email@xx.com'):\n",
    "    #登录邮箱，设置登录的账号，密码和port等信息\n",
    "    yag = yagmail.SMTP(user = 'youremail@sohu.com',password = 'yourpass',\n",
    "                       host = 'smtp.sohu.com',port = '465')\n",
    "    \n",
    "    #登录完即可一件发送，设置发送给谁，和邮件主题，邮件内容\n",
    "    yag.send(to = send_to,\n",
    "             subject = '天气关怀',\n",
    "             contents = contents)\n",
    "    print('发送成功！~')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 最终执行，设置自己的邮箱名，密码，host和port参数，以及要发给谁"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "send_email(weather,send_to = 'xxxx')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
